// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.commands;

import org.dengues.core.metadata.MetadataHelper;
import org.dengues.designer.ui.editors.AbstractGEFCommand;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.model.database.DBColumn;
import org.dengues.model.database.DBTable;
import org.eclipse.gef.requests.CreateRequest;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class DBColumnCreateCommand extends AbstractGEFCommand {

    private final DBTable table;

    private boolean dbColumnAdded;

    private final DBColumn newColumn;

    /**
     * Qiang.Zhang.Adolf@gmail.com DBColumnCreateCommand constructor comment.
     * 
     * @param table
     * @param request
     */
    public DBColumnCreateCommand(DBTable table, CreateRequest request) {
        if (table == null || request == null || !(request.getNewObject() instanceof DBColumn)) {
            throw new IllegalArgumentException();
        }
        this.table = table;
        newColumn = (DBColumn) request.getNewObject();
        setLabel(Messages.getString("DBColumnCreateCommand.Label")); //$NON-NLS-1$
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com DBColumnCreateCommand constructor comment.
     * 
     * @param table
     * @param column
     */
    public DBColumnCreateCommand(DBTable table, DBColumn column) {
        if (table == null || column == null) {
            throw new IllegalArgumentException();
        }
        this.table = table;
        newColumn = column;
        setLabel(Messages.getString("DBColumnCreateCommand.Label")); //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#execute()
     */
    @Override
    public void execute() {

        newColumn.setHeight(20);
        newColumn.setWidth(table.getWidth());
        newColumn.setX(0);
        newColumn.setY(computeY());
        newColumn.setName(MetadataHelper.generateNewColumnName(table, newColumn.getName()));
        dbColumnAdded = table.getColumns().add(newColumn);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "computeY".
     * 
     * @return
     */
    private int computeY() {
        int y = table.getY();
        for (Object co : table.getColumns()) {
            if (co instanceof DBColumn) {
                DBColumn column = (DBColumn) co;
                y += column.getHeight();
            }
        }
        return y;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#canUndo()
     */
    @Override
    public boolean canUndo() {
        return dbColumnAdded;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.commands.Command#undo()
     */
    @Override
    public void undo() {
        table.getColumns().remove(newColumn);
    }
}
